package com.ai.wx.http;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import com.ai.core.util.Arith;
import com.ai.wx.entity.MobileUserInfo;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class CmccSHHttpPostHelper {

	public static String requestSHCmccSmsCode(String mobileNumber) throws Exception {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		String url = null;
		HttpGet httpGet = null;
		InputStream is = null;
		Scanner sc = null;
		CloseableHttpResponse response1 = null;
		// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//
		// 设置日期格式
		try {
			/*************** step1:发送短信 ***************************/
			url = "https://sh.ac.10086.cn/loginex?act=1&telno=" + mobileNumber;
			httpGet = new HttpGet(url);
			response1 = httpclient.execute(httpGet);
			is = response1.getEntity().getContent();
			// sc =new Scanner(is);
			sc = new Scanner(new BufferedReader(new InputStreamReader(is, "UTF-8")));

			String SendStr = sc.nextLine();
			System.out.println(SendStr);
			// JSONObject dataJson = JSONObject.parseObject(SendStr);

			return SendStr;
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} finally {
			if (httpclient != null) {
				httpclient.close();
			}
			if (sc != null) {
				sc.close();
			}
			if (is != null) {
				is.close();
			}
			if (response1 != null) {
				response1.close();
			}

		}
		return null;
	}

	public static MobileUserInfo doLoginSHCmcc(String mobileNumber, String code) throws Exception {

		CloseableHttpClient httpclient = HttpClients.createDefault();
 		HttpGet httpGet = null;
		InputStream is = null;
		Scanner sc = null;
		CloseableHttpResponse response = null;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		MobileUserInfo userInfo = new MobileUserInfo();// 拼装返回信息
		try {

			String loginstr1 = "https://sh.ac.10086.cn/loginex?act=2&telno=" + mobileNumber + "&password=" + code + "&authLevel=1&validcode=&ctype=1&t=";
			System.out.println("begin:" + df.format(new Date()));// new
																 // Date()为获取当前系统时间
			httpGet = new HttpGet(loginstr1);
			response = httpclient.execute(httpGet);
			is = response.getEntity().getContent();
			sc = new Scanner(new BufferedReader(new InputStreamReader(is, "UTF-8")));
			JSONObject dataJson1 = JSONObject.parseObject(sc.nextLine());
			if ((dataJson1.getString("result").equals("1"))) {
				userInfo.setResult("1");
				userInfo.setMsg(dataJson1.getString("message"));
				System.out.println(dataJson1.toJSONString());
			} else {
				// 登录
				String loginstr2 = "http://www.sh.10086.cn/sh/wsyyt/ac/forward.jsp?uid=" + dataJson1.getString("uid");
				httpGet = new HttpGet(loginstr2);
				response = httpclient.execute(httpGet);

				/*************** step4:取流量 ***************************/
				httpGet = new HttpGet("http://www.sh.10086.cn/sh/wsyyt/action?act=my.getfreeres.liulzq");
				response = httpclient.execute(httpGet);
				is = response.getEntity().getContent();
				sc = new Scanner(new BufferedReader(new InputStreamReader(is, "UTF-8")));
				JSONObject stillFlow = JSONObject.parseObject(sc.nextLine()); 
				System.out.println(stillFlow.toJSONString());
				String stillStr;
				int flowValue = 0;
				if (stillFlow.getJSONObject("error").getString("code").equals("0")) {
					JSONObject valueObj= stillFlow.getJSONObject("value");
					JSONArray freeResPackList = valueObj.getJSONArray("freeResPackList");
					 
					for (int resPackIndex=0; resPackIndex< freeResPackList.size();resPackIndex++) {
						JSONObject freeResPack = freeResPackList.getJSONObject(resPackIndex);
						if(freeResPack.getIntValue("categoryId") == 2){//gprs
							JSONArray freeResList = freeResPack.getJSONArray("freeResList");
							for (int resIndex=0; resIndex< freeResList.size();resIndex++) {
								JSONObject freeRes = freeResList.getJSONObject(resIndex);
								 flowValue += freeRes.getIntValue("llStillNu");
							}
						}
                    }
					stillStr = Arith.div(flowValue, 1024, 2)+"";
					//stillStr = stillFlow.getJSONObject("value").getJSONArray("freeResPackList").getJSONObject(0).getJSONArray("freelist").getJSONObject(0).getString("stillStr");
				} else {
					stillStr = "0";
				}

				/*************** step4:取余额 ***************************/
				httpGet = new HttpGet("http://www.sh.10086.cn/sh/wsyyt/action?act=my.getaccountinfo");
				response = httpclient.execute(httpGet);
				is = response.getEntity().getContent();
				sc = new Scanner(new BufferedReader(new InputStreamReader(is, "UTF-8")));
				JSONObject usefee = JSONObject.parseObject(sc.nextLine());
 				System.out.println(usefee.toJSONString());
				String money;
				if (usefee.getJSONObject("error").getString("code").equals("0")) {
					money = usefee.getJSONObject("value").getString("usable_fee");
				} else {
					money = "00.00";
				}

				userInfo.setResult("0");
				userInfo.setPhone(mobileNumber);
				userInfo.setCurrentTime(df.format(new Date()));
				userInfo.setMoney(money);
				userInfo.setRemainFlow(stillStr);

			}
		} catch (ClientProtocolException e) {
			userInfo.setResult("1");
			userInfo.setMsg(e.getMessage());
			e.printStackTrace();
		} finally {
			if (httpclient != null) {
				httpclient.close();
			}
			if (sc != null) {
				sc.close();
			}
			if (is != null) {
				is.close();
			}
			if (response != null) {
				response.close();
			}
		}
		return userInfo;
	}
}
